/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client.bean;

import client.dal.CategoryDAL;
import client.dal.CvDAL;
import client.dal.LevelJobDAL;
import client.dal.LocationDAL;
import client.dal.WorkerDAL;
import client.entity.Category;
import client.entity.Cv;
import client.entity.CvCategory;
import client.entity.CvLocation;
import client.entity.Education;
import client.entity.EmploymentHistory;
import client.entity.LevelJob;
import client.entity.Location;
import client.entity.Worker;
import java.util.ArrayList;
import java.util.Map;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author mrlucky
 */
@ManagedBean
@SessionScoped
public class WorkerEditCV {

    private Cv cv;
    private Worker worker;
    private String gender_temp;
    private String dayofbirth;
    private String monofbirth;
    private String yearofbirth;
    private String dayofis;
    private String monofis;
    private String yearofis;
    private Education edu;
    private String monthofFromEdu = "January";
    private String yearofFromEdu = "1988";
    private String monthofToEdu = "January";
    private String yearofToEdu = "1988";
    private ArrayList<Education> listEdu;
    private EmploymentHistory emhis;
    private String monthofFromEmHis;
    private String yearofFromEmHis;
    private String monthofToEmHis;
    private String yearofToEmHis;
    private ArrayList<EmploymentHistory> listEmpHist;
    private ArrayList<String> cate_temp;
    private ArrayList<String> subCate;
    private ArrayList<String> loc_temp;
    private ArrayList<String> subLoc;
    public String lv_temp;
    private String monthlyIncome;
    private String joj_temp;
    private String cvId;

    public Cv getCv() {
        if (cv == null) {
            cvId = getParam("cvID");
            cv = new CvDAL().loadCvById(cvId);
            worker = new WorkerDAL().loadWorkerById(cv.getWorker().getId());
            if (worker.getGender()) {
                gender_temp = "1";
            } else {
                gender_temp = "0";
            }
            String[] sBD = cv.getBirthDay().split("-");
            dayofbirth = sBD[0];
            monofbirth = sBD[1];
            yearofbirth = sBD[2];
            String[] sIs = cv.getDateIssueIdcard().split("-");
            dayofis = sIs[0];
            monofis = sIs[1];
            yearofis = sIs[2];
            lv_temp = new LevelJobDAL().getLevelJobByID(String.valueOf(cv.getLevelJob().getId())).getName();
            listEdu = new CvDAL().loadEduByCvId(cv.getId());
            listEmpHist = new CvDAL().loadEmpHistByCvId(cv.getId());
            if (cv.getTypeJob()) {
                joj_temp = "1";
            } else {
                joj_temp = "0";
            }
        }
        return cv;
    }

    public void setCv(Cv cv) {
        this.cv = cv;
    }

    public String getJoj_temp() {
        return joj_temp;
    }

    public void setJoj_temp(String joj_temp) {
        this.joj_temp = joj_temp;
    }

    public String getMonthlyIncome() {
        return monthlyIncome;
    }

    public void setMonthlyIncome(String monthlyIncome) {
        this.monthlyIncome = monthlyIncome;
    }

    public String getLv_temp() {
        return lv_temp;
    }

    public void setLv_temp(String lv_temp) {
        this.lv_temp = lv_temp;
    }

    public ArrayList<LevelJob> getListLJ() {
        return new LevelJobDAL().loadLevelJob();
    }

    public ArrayList<String> getSubLoc() {
        if (subLoc == null) {
            subLoc = new ArrayList<String>();
        }
        return subLoc;
    }

    public void setSubLoc(ArrayList<String> subLoc) {
        this.subLoc = subLoc;
    }

    public ArrayList<String> getLoc_temp() {
        if (loc_temp == null) {
            loc_temp = new ArrayList<String>();
        }
        return loc_temp;
    }

    public void setLoc_temp(ArrayList<String> loc_temp) {
        this.loc_temp = loc_temp;
    }

    public ArrayList<Location> getLoc() {
        return new LocationDAL().listLoca();
    }

    public ArrayList<String> getSubCate() {
        if (subCate == null) {
            subCate = new ArrayList<String>();
        }
        return subCate;
    }

    public void setSubCate(ArrayList<String> subCate) {
        this.subCate = subCate;
    }

    public ArrayList<String> getCate_temp() {
        if (cate_temp == null) {
            cate_temp = new ArrayList<String>();
        }
        return cate_temp;
    }

    public void setCate_temp(ArrayList<String> cate_temp) {
        this.cate_temp = cate_temp;
    }

    public ArrayList<Category> getCate() {
        return new CategoryDAL().listCate();
    }

    public ArrayList<EmploymentHistory> getListEmpHist() {
        if (listEmpHist == null) {
            listEmpHist = new ArrayList<EmploymentHistory>();
        }
        return listEmpHist;
    }

    public void setListEmpHist(ArrayList<EmploymentHistory> listEmpHist) {
        this.listEmpHist = listEmpHist;
    }

    public ArrayList<Education> getListEdu() {
        if (listEdu == null) {
            listEdu = new ArrayList<Education>();
        }
        return listEdu;
    }

    public void setListEdu(ArrayList<Education> listEdu) {
        this.listEdu = listEdu;
    }

    public String getMonthofFromEmHis() {
        return monthofFromEmHis;
    }

    public void setMonthofFromEmHis(String monthofFromEmHis) {
        this.monthofFromEmHis = monthofFromEmHis;
    }

    public String getYearofFromEmHis() {
        return yearofFromEmHis;
    }

    public void setYearofFromEmHis(String yearofFromEmHis) {
        this.yearofFromEmHis = yearofFromEmHis;
    }

    public String getMonthofToEmHis() {
        return monthofToEmHis;
    }

    public void setMonthofToEmHis(String monthofToEmHis) {
        this.monthofToEmHis = monthofToEmHis;
    }

    public String getYearofToEmHis() {
        return yearofToEmHis;
    }

    public void setYearofToEmHis(String yearofToEmHis) {
        this.yearofToEmHis = yearofToEmHis;
    }

    public String getYearofFromEdu() {
        return yearofFromEdu;
    }

    public void setYearofFromEdu(String yearofFromEdu) {
        this.yearofFromEdu = yearofFromEdu;
    }

    public String getMonthofToEdu() {
        return monthofToEdu;
    }

    public void setMonthofToEdu(String monthofToEdu) {
        this.monthofToEdu = monthofToEdu;
    }

    public String getYearofToEdu() {
        return yearofToEdu;
    }

    public void setYearofToEdu(String yearofToEdu) {
        this.yearofToEdu = yearofToEdu;
    }

    public String getMonthofFromEdu() {
        return monthofFromEdu;
    }

    public void setMonthofFromEdu(String monthofFromEdu) {
        this.monthofFromEdu = monthofFromEdu;
    }

    public Education getEdu() {
        if (edu == null) {
            edu = new Education();
        }
        return edu;
    }

    public void setEdu(Education edu) {
        this.edu = edu;
    }

    public Worker getWorker() {
        return worker;
    }

    public void setWorker(Worker worker) {
        this.worker = worker;
    }

    public String getGender_temp() {
        return gender_temp;
    }

    public void setGender_temp(String gender_temp) {
        this.gender_temp = gender_temp;
    }

    public String getDayofbirth() {
        return dayofbirth;
    }

    public void setDayofbirth(String dayofbirth) {
        this.dayofbirth = dayofbirth;
    }

    public String getMonofbirth() {
        return monofbirth;
    }

    public void setMonofbirth(String monofbirth) {
        this.monofbirth = monofbirth;
    }

    public String getYearofbirth() {
        return yearofbirth;
    }

    public void setYearofbirth(String yearofbirth) {
        this.yearofbirth = yearofbirth;
    }

    public String getDayofis() {
        return dayofis;
    }

    public void setDayofis(String dayofis) {
        this.dayofis = dayofis;
    }

    public String getMonofis() {
        return monofis;
    }

    public void setMonofis(String monofis) {
        this.monofis = monofis;
    }

    public String getYearofis() {
        return yearofis;
    }

    public void setYearofis(String yearofis) {
        this.yearofis = yearofis;
    }

    public EmploymentHistory getEmhis() {
        if (emhis == null) {
            emhis = new EmploymentHistory();
        }
        return emhis;
    }

    public void setEmhis(EmploymentHistory emhis) {
        this.emhis = emhis;
    }

    /**
     * Creates a new instance of WorkerEditCV
     */
    public WorkerEditCV() {
    }

    public void addEduToList() {
        if (!edu.getNameSchool().isEmpty()) {
            edu.setFromDate(monthofFromEdu + "-" + yearofFromEdu);
            edu.setToDate(monthofToEdu + "-" + yearofToEdu);
            Education e = new Education(null, edu.getNameSchool(), edu.getDegeeLevel(), edu.getFildOfStudy(), edu.getFromDate(), edu.getToDate(), edu.getAchiement());
            listEdu.add(e);
        }
    }

    public void deleteEduToList(Education edu) {
        for (int i = 0; i < listEdu.size(); i++) {
            if (listEdu.get(i).equals(edu)) {
                listEdu.remove(i);
                break;
            }
        }
    }

    public void addEmpToList() {
        if (!emhis.getCompany().isEmpty()) {
            emhis.setFromDate(monthofFromEmHis + "-" + yearofFromEmHis);
            emhis.setToDate(monthofToEmHis + "-" + yearofToEmHis);
            EmploymentHistory empHist = new EmploymentHistory(null, emhis.getCategory(), emhis.getJobLevel(), emhis.getCompany(), emhis.getFromDate(), emhis.getToDate(), emhis.getMainDuties(), emhis.getResonLeaving());
            listEmpHist.add(empHist);
        }
    }

    public void deleteEmpToList(EmploymentHistory empHist) {
        for (int i = 0; i < listEmpHist.size(); i++) {
            if (listEmpHist.get(i).equals(empHist)) {
                listEmpHist.remove(i);
                break;
            }
        }
    }

    public void insertCate() {
        if (subCate.size() < 3) {
            for (String s : cate_temp) {
                subCate.add(s);
            }
        }
    }

    public void deleteCate(String s) {
        for (int i = 0; i < subCate.size(); i++) {
            if (s.equals(subCate.get(i))) {
                subCate.remove(i);
            }
        }
    }

    public void insertLoc() {
        if (subLoc.size() < 3) {
            for (String s : loc_temp) {
                subLoc.add(s);
            }
        }
    }

    public void deleteLoc(String s) {
        for (int i = 0; i < subLoc.size(); i++) {
            if (s.equals(subLoc.get(i))) {
                subLoc.remove(i);
            }
        }
    }

    public String workerEditCV() {
        if (subCate.isEmpty()) {
            FacesContext.getCurrentInstance().addMessage("formId:select1", new FacesMessage("   * Desired Category is required"));
            return "";
        }
        if (subLoc.isEmpty()) {
            FacesContext.getCurrentInstance().addMessage("formId:select2", new FacesMessage("   * Desired Location is required"));
            return "";
        }
        boolean gen = false;
        if (gender_temp.equals("1")) {
            gen = true;
            gender_temp = "Male";
        } else {
            gender_temp = "Female";
        }
        worker.setGender(gen);
        new WorkerDAL().updateWorker(worker);
        LevelJob lv = new LevelJobDAL().getLevelJobByID(lv_temp);
        cv.setBirthDay(dayofbirth + "-" + monofbirth + "-" + yearofbirth);
        cv.setDateIssueIdcard(dayofis + "-" + monofis + "-" + yearofis);
        String income = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("test");
        if (income.equals("radio1")) {
            cv.setMoneyIncome("Agreement");
        } else if (income.equals("radio2")) {
            cv.setMoneyIncome(monthlyIncome);
        }
        boolean toj = false;
        if (joj_temp.equals("1")) {
            toj = true;
        }
        cv.setTypeJob(toj);
        cv.setWorker(worker);
        cv.setLevelJob(lv);
        cv.setActive(Boolean.FALSE);
        if (!listEdu.isEmpty()) {
            for (Education e : listEdu) {
                e.setCv(cv);
            }
        }
        if (!listEmpHist.isEmpty()) {
            for (EmploymentHistory eh : listEmpHist) {
                eh.setCv(cv);
            }
        }
        ArrayList<CvCategory> listCC_temp = new ArrayList<CvCategory>();
        for (String s : subCate) {
            CvCategory cc = new CvCategory(cv, listCateConvert(s));
            listCC_temp.add(cc);
        }
        ArrayList<CvLocation> listCL_temp = new ArrayList<CvLocation>();
        for (String ss : subLoc) {
            CvLocation cl = new CvLocation(cv, listLocaConvert(ss));
            listCL_temp.add(cl);
        }
        new CvDAL().updateCV(cv);
        new CvDAL().deleteCvCateByRecId(cv.getId());
        new CvDAL().insertCvCate(listCC_temp);
        new CvDAL().deleteCvLocByRecId(cv.getId());
        new CvDAL().insertCvLoca(listCL_temp);
        if (!listEdu.isEmpty()) {
            new WorkerDAL().deleteEducationByCvId(cv.getId());
            new WorkerDAL().insertEducation(listEdu);
        }
        if (!listEmpHist.isEmpty()) {
            new WorkerDAL().deleteEmployHistoryByCvId(cv.getId());
            new WorkerDAL().insertEmployHistory(listEmpHist);
        }
        HttpSession ss = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        ss.removeAttribute("workerProfile");
        return "workerProfile.xhtml?faces-redirect=true";
    }

    public String backToProfile() {
        HttpSession ss = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        ss.removeAttribute("workerProfile");
        return "workerProfile.xhtml?faces-redirect=true";
    }

    public Category listCateConvert(String s) {
        ArrayList<Category> list_temp = new CategoryDAL().listCate();
        for (Category c : list_temp) {
            if (s.equals(c.getCategoryName())) {
                return c;
            }
        }
        return null;
    }

    public Location listLocaConvert(String s) {
        ArrayList<Location> list_temp = new LocationDAL().listLoca();
        for (Location l : list_temp) {
            if (s.equals(l.getName())) {
                return l;
            }
        }
        return null;
    }

    public String getParam(String param) {

        Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        return params.get(param);

    }
}
